//
//  UserRegisterViewController.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/9.
//

import UIKit

enum Myerror:Error
{
    case alreadyUsed(String)
    case tooLong(String)
    
}
class UserRegisterViewController: UIViewController {

    @IBOutlet weak var errorLabel: UILabel!
    
    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var userPwd: UITextField!
    
    @IBOutlet weak var pwdCheck: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    @IBAction func EnterButtonPressed(_ sender: UIButton) {
        var legalRegister:Bool = true
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let queryResult = UserTable.getUserName(userName.text!)
        sqlite.closeDB();
        if queryResult != nil
        {
            errorLabel.text="用户名已经被注册了！"
            legalRegister=false
        }
        if userName.text!.count > 20
        {
            errorLabel.text="用户名长度不能超过20！"
            legalRegister=false
        }
        if userPwd.text!.count > 20
        {
            errorLabel.text="密码长度不能超过20！"
            legalRegister=false
        }
        if userPwd.text! != pwdCheck.text!
        {
            errorLabel.text="两次密码不匹配！"
            legalRegister=false
        }
        if legalRegister
        {
            let user = UserInfo(name0: userName.text!, pwd0: userPwd.text!)
            UserTable.InsertUserInfo(user: user)
            self.dismiss(animated: true, completion: nil)
        }
        
    }
    @IBAction func tap(_ sender: UITapGestureRecognizer) {
        userName.resignFirstResponder()
        userPwd.resignFirstResponder()
        pwdCheck.resignFirstResponder()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
